﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Util
{
    public static class DataSwitch
    {
        /// <summary>
        /// 将数字的对象表示形式转换为它的等效 32 位有符号整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int32 ConvertObjectToInt32(object value)
        {
            try
            {
                if (value != null)
                {
                    return Int32.Parse(value.ToString().Trim());
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将数字的对象表示形式转换为它的等效 64 位有符号整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int64 ConvertObjectToInt64(object value)
        {
            try
            {
                if (value != null)
                {
                    return Int64.Parse(value.ToString().Trim());
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将数字的对象表示形式转换为它的等效双精度浮点数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Double ConvertObjectToDouble(object value)
        {
            try
            {
                if (value != null)
                {
                    return Double.Parse(value.ToString().Trim());
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将数字的对象表示形式转换为其等效的 Decimal 形式
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Decimal ConvertObjectToDecimal(object value)
        {
            try
            {
                if (value != null)
                {
                    return Decimal.Parse(value.ToString().Trim());
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static String ConvertObjectToString(object value)
        {
            try
            {
                if (value != null)
                {
                    return value.ToString().Trim();
                }
                else
                {
                    return "";
                }
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 将日期和时间的指定对象表示转换成其等效的 DateTime
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime ConvertObjectToDateTime(object value)
        {
            try
            {
                if (value != null)
                {
                    return DateTime.Parse(value.ToString().Trim());
                }
                else
                {
                    return DateTime.Parse("1900-1-1");
                }
            }
            catch
            {
                return DateTime.Parse("1900-1-1");
            }
        }

        /// <summary>
        /// 将逻辑值的指定对象表格形式转换为它的等效 System.Boolean 值。
        /// </summary>
        /// <param name="value">包含要转换的值的对象</param>
        /// <returns>Boolean</returns>
        public static Boolean ConvertObjectToBoolean(object value)
        {
            try
            {
                if (value != null)
                {
                    return Boolean.Parse(value.ToString().Trim());
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 将数字的对象表示形式转换为其等效的 float 形式
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static float ConverObjectToFloat(object value)
        {
            try
            {
                if (value != null)
                {
                    return float.Parse(value.ToString().Trim());
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

        /////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 将数字的对象表示形式转换为它的等效 32 位有符号整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int32 ConvertObjectToInt32(object value, Int32 defaultValue)
        {
            try
            {
                if (value != null)
                {
                    return Int32.Parse(value.ToString().Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将数字的对象表示形式转换为它的等效 64 位有符号整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int64 ConvertObjectToInt64(object value, Int64 defaultValue)
        {
            try
            {
                if (value != null)
                {
                    return Int64.Parse(value.ToString().Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将数字的对象表示形式转换为它的等效双精度浮点数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Double ConvertObjectToDouble(object value, Double defaultValue)
        {
            try
            {
                if (value != null)
                {
                    return Double.Parse(value.ToString().Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将数字的对象表示形式转换为其等效的 Decimal 形式
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Decimal ConvertObjectToDecimal(object value, Decimal defaultValue)
        {
            try
            {
                if (value != null)
                {
                    return Decimal.Parse(value.ToString().Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static String ConvertObjectToString(object value, String defaultValue)
        {
            try
            {
                if (value != null)
                {
                    return value.ToString().Trim();
                }
                else
                {
                    return defaultValue;
                }
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 将日期和时间的指定对象表示转换成其等效的 DateTime
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime ConvertObjectToDateTime(object value, DateTime defaultValue)
        {
            try
            {
                if (value != null)
                {
                    return DateTime.Parse(value.ToString().Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 将逻辑值的指定对象表格形式转换为它的等效 System.Boolean 值。
        /// </summary>
        /// <param name="value">包含要转换的值的对象</param>
        /// <returns>Boolean</returns>
        public static Boolean ConvertObjectToBoolean(object value, Boolean defaultValue)
        {
            try
            {
                if (value != null)
                {
                    return Boolean.Parse(value.ToString().Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 创建人：焦元超
        /// 创建时间:2009-5-11
        /// 功能:DataTable 转换为业务实体集合
        /// 注:实体属性数据类型:只支持系统的常用类型(int,string, bool,flaot,double),不支持(Enum,自定义,集合等类型)
        /// <![CDATA[List<entity> entityList = Util.DataSwitch.ConvertDataTableToEntityCollections<entityType>(dataRowsDT);]]>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> ConvertDataTableToEntityCollections<T>(DataTable dt) where T : class, new()
        {
            List<T> modellist = new List<T>();
            try
            {
                if (dt != null)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        T o = new T();
                        o = ConvertDataTableToEntity<T>(dr);
                        modellist.Add((T)o);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DataTable 转换为业务实体集合出错，原因:" + ex.Message);
            }
            return modellist;
        }
        /// <summary>
        /// 创建人：焦元超
        /// 创建时间:2009-5-11
        /// 功能:DataTable 转换为业务实体 
        /// 注:实体属性数据类型:只支持系统的常用类型(int,string, bool,flaot,double),不支持(Enum,自定义,集合等类型)
        /// <![CDATA[ entity = Util.DataSwitch.ConvertDataTableToEntity<entityType>(dataRowsDT);]]>
        public static T ConvertDataTableToEntity<T>(DataRow row) where T : class, new()
        {
           
            T objEntity = new T();
            try
            {
                System.Reflection.PropertyInfo[] p = objEntity.GetType().GetProperties();
                foreach (System.Reflection.PropertyInfo pi in p)
                {
                    Type t = System.Type.GetType(pi.PropertyType.ToString());
                    if (t != null)
                    {
                        if (row.Table.Columns.Contains(pi.Name))
                        {
                            pi.SetValue(objEntity, GetDefaultValue(row[pi.Name], pi.PropertyType), null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DataTable 转换为业务实体错误原因:" + ex.Message);
            }
            return objEntity;
        }
        /// <summary>
        /// 创建人：焦元超
        /// 创建时间:2009-5-11
        /// 获取类型的默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static object GetDefaultValue(object obj, Type type)
        {

            if (obj == DBNull.Value)
            {
                obj = default(object);
            }
            else
            {
                obj = Convert.ChangeType(obj, type);
            }
            return obj;
        }




    }
}
